<?php
/**
 * Created by PhpStorm.
 * User: dust2
 * Date: 15-1-5
 * Time: 下午3:33
 */

class Role extends Eloquent
{
    public function __construct()
    {
        $this->currUser = Auth::user();
    }

    /**
     * 获取用户所有角色列表清单
     * @return array
     */
    public static function getUserRoleNames()
    {
        return UserRole::where('user_id', Auth::user()['id'])
            ->leftJoin('roles', 'user_role.role_id', '=', 'roles.id')
            ->select('name')
            ->get()
            ->toArray();
    }

    /**
     * 校验用户是否拥有角色
     * @param $name
     * @return bool
     */
    public static function checkIdentify($name)
    {
        foreach (Role::getUserRoleNames() as $per) {
            if (in_array($name, $per)) {
                return true;
            }
        }

        return false;
    }


    protected $table = 'roles';
    protected $guarded = array('id');

}